package com.hethongviet.vending;

import android.app.Activity;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;

import com.hethongviet.vending.helper.call.ListenPhoneCallService;

public final class Utils
{

	public static boolean disableAllKey(KeyEvent event)
	{
		if (KeyEvent.KEYCODE_SEARCH == event.getKeyCode()
				&& (event.getFlags() & KeyEvent.FLAG_LONG_PRESS) == KeyEvent.FLAG_LONG_PRESS)
		{
			return true;
		}
		if (KeyEvent.KEYCODE_HOME == event.getKeyCode()
				&& (event.getFlags() & KeyEvent.FLAG_LONG_PRESS) == KeyEvent.FLAG_LONG_PRESS)
		{
			return true;
		}

		if (KeyEvent.KEYCODE_MENU == event.getKeyCode()
				|| KeyEvent.KEYCODE_DPAD_LEFT == event.getKeyCode()
				|| KeyEvent.KEYCODE_DPAD_DOWN == event.getKeyCode()
				|| KeyEvent.KEYCODE_DPAD_RIGHT == event.getKeyCode()
				|| KeyEvent.KEYCODE_DPAD_UP == event.getKeyCode()
				|| KeyEvent.KEYCODE_DPAD_CENTER == event.getKeyCode()
				|| KeyEvent.KEYCODE_CALL == event.getKeyCode()
				|| KeyEvent.KEYCODE_CAMERA == event.getKeyCode()
				|| KeyEvent.KEYCODE_ENDCALL == event.getKeyCode()
				|| KeyEvent.KEYCODE_HOME == event.getKeyCode()
				|| KeyEvent.KEYCODE_SEARCH == event.getKeyCode()
				|| KeyEvent.KEYCODE_BACK == event.getKeyCode())
		{
			return false;
		}
		return true;
	}

	public static boolean disableFunctionsKey(KeyEvent event)
	{
		if (KeyEvent.KEYCODE_SEARCH == event.getKeyCode()
				&& (event.getFlags() & KeyEvent.FLAG_LONG_PRESS) == KeyEvent.FLAG_LONG_PRESS)
		{
			return true;
		}
		if (KeyEvent.KEYCODE_HOME == event.getKeyCode()
				&& (event.getFlags() & KeyEvent.FLAG_LONG_PRESS) == KeyEvent.FLAG_LONG_PRESS)
		{
			return true;
		}

		if (KeyEvent.KEYCODE_MENU == event.getKeyCode()
				|| KeyEvent.KEYCODE_DPAD_LEFT == event.getKeyCode()
				|| KeyEvent.KEYCODE_DPAD_DOWN == event.getKeyCode()
				|| KeyEvent.KEYCODE_DPAD_RIGHT == event.getKeyCode()
				|| KeyEvent.KEYCODE_DPAD_UP == event.getKeyCode()
				|| KeyEvent.KEYCODE_DPAD_CENTER == event.getKeyCode()
				|| KeyEvent.KEYCODE_CALL == event.getKeyCode()
				|| KeyEvent.KEYCODE_CAMERA == event.getKeyCode()
				|| KeyEvent.KEYCODE_ENDCALL == event.getKeyCode()
				|| KeyEvent.KEYCODE_HOME == event.getKeyCode()
				|| KeyEvent.KEYCODE_SEARCH == event.getKeyCode()
				|| KeyEvent.KEYCODE_BACK == event.getKeyCode())
		{
			return false;
		}
		return true;
	}

	public static int getInSampleSize(BitmapFactory.Options options, int reqWidth,
			int reqHeight)
	{
		// Raw height and width of image
		final int height = options.outHeight;
		final int width = options.outWidth;
		int inSampleSize = 1;
		/*
		 * get the scale it fix with the new size but the image will be no
		 * resized
		 */
		int width2 = width >> 1;
		int height2 = height >> 1;
		while ((width2 / inSampleSize) >= reqWidth
				|| (height2 / inSampleSize) >= reqHeight)
		{
			inSampleSize <<= 1;
		}

		return inSampleSize;
	}

	public static boolean isSdCardPresent()
	{
		return android.os.Environment.getExternalStorageState().equalsIgnoreCase(
				android.os.Environment.MEDIA_MOUNTED);
	}

	public static void prepareWindow(Activity activity)
	{
		activity.getWindow().addFlags(
				WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

		if (AppConfig.IS_FULL_SCREEN) // making full screen
		{
			activity.requestWindowFeature(Window.FEATURE_NO_TITLE);
			activity.getWindow().setFlags(
					WindowManager.LayoutParams.FLAG_FULLSCREEN,
					WindowManager.LayoutParams.FLAG_FULLSCREEN);
		}

		/* Start the service of listening phone call */
		activity.startService(new Intent(activity, ListenPhoneCallService.class));
	}

	public static void debug(String string)
	{
		if (AppConfig.IS_DEBUG)
		{
			Log.d("Debug", "@@@@@@@@@@@@@@@@@@@@@@@ " + string);
		}
	}

}
